package com.drugStorage.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.drugStorage.domain.DrugSupplier;
import com.drugStorage.hibernate.Utils.HibernateUtils;
import com.drugStorage.hibernate.dao.DrugSupplierDao;

public class DrugSupplierDaoImpl implements DrugSupplierDao {
	
	private static final String LOADS_ALL = "from DrugSupplier";

	public void delDrugSupplier(DrugSupplier drugSupplier) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.delete(drugSupplier);
			tx.commit();
		} finally {
			if (s != null)
				s.close();
		}
		
	}
	

	public DrugSupplier getDrugSupplierById(int id) {
		Session s = null;

		try {
			s = HibernateUtils.getSession();

			Criteria c = s.createCriteria(DrugSupplier.class);
			c.add(Restrictions.eq("id", id));
			DrugSupplier drugSupplier = (DrugSupplier) c.uniqueResult();
			return drugSupplier;

		} finally {
			if (s != null)
				s.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<DrugSupplier> getDrugSuppliers() {
		Session s = null;
		try {
			s = HibernateUtils.getSession();
			Query q = s.createQuery(LOADS_ALL);

			return q.list();

		} finally {
			if (s != null)
				s.close();

		}
	}

	public void saveDrugSupplier(DrugSupplier drugSupplier) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.persist(drugSupplier);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}
	}

	public void updateDrugSupplier(DrugSupplier drugSupplier) {
		Session s = null;
		Transaction tx = null;
		try {
			s = HibernateUtils.getSession();
			tx = s.beginTransaction();
			s.saveOrUpdate(drugSupplier);
			tx.commit();
		} finally {
			if (s != null)
				s.close();

		}

	}
		
	

}
